﻿using System;
using System.Workflow.Activities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;

namespace TPG.SharePoint.Workflow.Sample
{
  public sealed partial class Workflow : SequentialWorkflowActivity
  {
    public string TaskOneResult;
    public string TaskOneComments;
    public string TaskTwoResult;
    public string TaskTwoComments;

    public Guid TaskId = default(Guid);
    public Guid TaskId2 = default(Guid);
    public String TaskTwoContentTypeID = default(System.String);

    public SPWorkflowActivationProperties WorkflowActivationProperties;
    public SampleConfiguration _configData;

    public Workflow()
    {
      InitializeComponent();
    }

    private void OnWorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
    {
      _configData = WorkflowDataSerializer.DeserializeFromXmlString<SampleConfiguration>(
          WorkflowActivationProperties.InitiationData);
      EnsureTaskContentType(ContentTypeID.TaskTwo);
    }

    private void CreateTaskOne_Invoking(object sender, EventArgs e)
    {
      TaskId = Guid.NewGuid();
      CreateTask activity = sender as CreateTask;

      activity.TaskProperties = new SPWorkflowTaskProperties();
      activity.TaskProperties.AssignedTo = _configData.Level1Approver;
      activity.TaskProperties.TaskType = 0;
      activity.TaskProperties.DueDate = DateTime.Now.AddDays(3);
      activity.TaskProperties.ExtendedProperties["Instructions"] = _configData.Instructions;
      activity.TaskProperties.Title = "Perform Task One";
    }

    private void ChangeTaskOne_Invoked(object sender, ExternalDataEventArgs e)
    {
      SPTaskServiceEventArgs args = e as SPTaskServiceEventArgs;
      TaskOneResult = (string)args.afterProperties.ExtendedProperties["Level1Result"];
      TaskOneComments = (string)args.afterProperties.ExtendedProperties["Level1Comments"];
    }

    private void CreateTaskTwo_Invoking(object sender, EventArgs e)
    {
      TaskId2 = Guid.NewGuid();
      TaskTwoContentTypeID = ContentTypeID.TaskTwo;
      CreateTaskWithContentType activity = sender as CreateTaskWithContentType;
      activity.TaskProperties = new SPWorkflowTaskProperties();
      activity.TaskProperties.AssignedTo = _configData.Level2Approver;
      activity.TaskProperties.TaskType = 1;
      activity.TaskProperties.DueDate = DateTime.Now.AddDays(3);
      activity.TaskProperties.ExtendedProperties["Instructions"] = _configData.Instructions;
      activity.TaskProperties.Title = "Perform Task Two";
    }

    private void ChangeTaskTwo_Invoked(object sender, ExternalDataEventArgs e)
    {
      SPTaskServiceEventArgs args = e as SPTaskServiceEventArgs;
      SPListItem taskItem = WorkflowActivationProperties.TaskList.GetItemById(args.afterProperties.TaskItemId);

      TaskTwoResult = (string)taskItem[FieldID.WSS3WorkflowSampleReviewStatus];
      TaskTwoComments = (string)taskItem[FieldID.WSS3WorkflowSampleReviewComments];
    }

    private void WorkflowModification_Invoking(object sender, EventArgs e)
    {
      EnableWorkflowModification activity = sender as EnableWorkflowModification;

      InstructionsModification data = new InstructionsModification();
      data.Instructions = _configData.Instructions;

      ModificationData typedData = ModificationData.CreateUsingTypedData(data);
      activity.ContextData = typedData.NativeData;
    }

    private void WaitForModification_Invoked(object sender,
        ExternalDataEventArgs e)
    {
      SPModificationEventArgs args = e as SPModificationEventArgs;
      ModificationData data = ModificationData.CreateUsingStringData(args.data);
      InstructionsModification typedData = data.GetTypedData<InstructionsModification>();
      // ...
    }
    void EnsureTaskContentType(string contentTypeIDString)
    {
      SPList taskList = WorkflowActivationProperties.TaskList;
      if (taskList.ContentTypesEnabled == false)
      {
        taskList.ContentTypesEnabled = true;
        taskList.Update();
      }
      SPContentTypeId contentTypeID = new SPContentTypeId(contentTypeIDString);
      SPContentTypeId activatedID = taskList.ContentTypes.BestMatch(contentTypeID);
      if (activatedID == null || activatedID.IsChildOf(contentTypeID) == false)
      {
        SPContentType contentType = WorkflowActivationProperties.Web.AvailableContentTypes[contentTypeID];
        taskList.ContentTypes.Add(contentType);
      }
    }

  }
}
